#include "udpclient.hpp"
#include "mythread.hpp"
#include <memory>

void usage(const string &procname)
{
    cerr << "Usage:\n\t";
    cerr << procname << " serverIp serverPort" << endl;
}

/*
void *receiveMessage(void *args)
{
    // 需要从args中获取套接字信息编号、服务器ip、服务器端口号
    unique_ptr<udpclient> &clientptr = *static_cast<unique_ptr<udpclient> *>(args);

    struct sockaddr_in serverInfor;
    socklen_t len = sizeof(serverInfor);
    bzero(&serverInfor, sizeof(serverInfor));
    serverInfor.sin_family = AF_INET;
    serverInfor.sin_port = htons(clientptr->get_serverPort());
    serverInfor.sin_addr.s_addr = inet_addr(clientptr->get_serverIp().c_str());

    char buffer[BUFSIZE];
    while (true)
    {
        int n = recvfrom(clientptr->get_sockfd(), buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&serverInfor, &len);
        // 打印翻译结果
        buffer[n] = 0;
        cout << buffer << endl;
    }
}
*/

// ./client serverIp serverPort
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }

    // 创建服务器
    unique_ptr<udpclient> clientptr(new udpclient(argv[1], stoi(argv[2])));
    clientptr->init();
    clientptr->start();

    return 0;
}